package hoan.mvc.controller;

import hoan.mvc.form.ContactListForm;
import hoan.mvc.model.Contact;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.MatrixVariable;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.SessionAttributes;

/**
 * @author Horizon Jul 24, 2013 4:54:04 PM
 * 
 */
@Controller
@RequestMapping(value = "/contact-list")
@SessionAttributes(value = "genders")
public class ContactListController {

	@Autowired
	private ContactListForm contactListForm;

	private static List<Contact> database = new ArrayList<Contact>();

	static {
		database.add(new Contact(1, "Jame", "Bond", "jame.bond@gmail.com", "0988XXXXXXX"));
		database.add(new Contact(2, "Jonh", "Smith", "jonh.smith@yahoo.com", "0909345YYYY"));
		database.add(new Contact(3, "Java", "Sun", "java.sun@sun.com", "082345XXXX"));
		database.add(new Contact(4, "Java", "FX", "java.fx@sun.com", "082345XXXX"));
		database.add(new Contact(5, "Net", "Mircosoft", "net.microsoft@microsoft.com", "08123456"));
		database.add(new Contact(6, "Ruby", "OnRail", "ruby@gmail.com", "0988123XXXXX"));
		database.add(new Contact(7, "Spring", "Java", "spring.java@java.com", "09883456XXXX"));
		database.add(new Contact(8, "Struts", "Java", "struts.java@java.com", "082245700000"));
		database.add(new Contact(9, "Hibernate", "Java", "hibernate.java@java.com", "080808"));
		database.add(new Contact(10,"Ibatis", "Java", "ibatis.java@java.com", "0606060606060"));
	}

	@ModelAttribute("genders")
	private List<Contact> listContacts() {

		List<Contact> contacts = new ArrayList<Contact>();
		contacts.add(new Contact(10,"Ibatis", "Java", "ibatis.java@java.com", "0606060606060"));
		return contacts;
	}

	/**
	 * 
	 * @param map
	 * @return
	 */
	@RequestMapping(value = "/getAll")
	public String initPage(ModelMap map) {

		contactListForm.setContactList(database);
		contactListForm.setContact(new Contact());
		map.addAttribute("contactListForm", contactListForm);

		return "contact-list";
	}

	/**
	 * 
	 * @param keyword
	 * @param map
	 * @return
	 */
	@RequestMapping(value = { "/search" })
	public String search(@RequestParam(value = "keyword") String keyword,
			@ModelAttribute(value = "genders") List<Contact> contacts, ModelMap map) {

		List<Contact> searchedContacts = new ArrayList<Contact>();
		for (Contact contact : database) {
			if (keyword.trim().equals("")
					|| contact.getFirstName().trim().toLowerCase().equals(keyword.trim().toLowerCase())) {
				searchedContacts.add(contact);
			}
		}

		contactListForm.setContactList(searchedContacts);
		contactListForm.setKeyword(keyword);
		map.addAttribute("contactListForm", contactListForm);

		contacts.clear();

		return "contact-list";
	}

	/**
	 * 
	 * @param contact
	 * @param map
	 * @return
	 */
	@RequestMapping(value = "/add")
	public String addContact(@ModelAttribute(value = "contactListForm") ContactListForm contactListForm,
			ModelMap map) {

		this.contactListForm.getContactList().add(contactListForm.getContact());
		this.contactListForm.setKeyword("");
		map.addAttribute("contactListForm", this.contactListForm);
		
		return "contact-list";
	}
	
	/**
	 * 
	 * @param contactId
	 * @param map
	 * @return
	 */
	@RequestMapping(value = "/delete/{contactId}", method = RequestMethod.DELETE)
	public String deleteContact(@PathVariable(value="contactId") String contactId, ModelMap map){
		
		List<Contact> formContacts = this.contactListForm.getContactList();
		for(int i = 0; i < formContacts.size(); i++){
			Contact contact = formContacts.get(i);
			if(contact.getId().toString().equalsIgnoreCase(contactId)){
				formContacts.remove(contact);
				i--;
			}
		}
		this.contactListForm.setContactList(formContacts);
		this.contactListForm.setKeyword("");
		map.addAttribute("contactListForm", this.contactListForm);

		return "contact-list";
	}

	
	/**
	 * 
	 * @param matrixMap
	 * @param map
	 * @return
	 */
	// http://localhost:8080/contact-list/searchMatrixMap;abc=a/ext;abc=b
	// -------------------> abc=a
	@RequestMapping(value = "/searchMatrixMap{q}/ext{z}")
	public String searchMatrixMap(@MatrixVariable(value = "abc", pathVar = "q") String abc, ModelMap map) {

		System.out.println("------------------------" + abc);
		return "contact-list";
	}
}
